package com.MC.MCard.servlet;

import com.MC.MCard.dao.MerchantDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MerchantRegisterServlet extends HttpServlet{
    private MerchantDaoImpl merchantDao = new MerchantDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  memail = req.getParameter("memail");
        String mname = req.getParameter("mname");
        String mpassword = req.getParameter("mpassword");

        long rows = merchantDao.insert(memail,mname,mpassword);
        if( rows > 0 ) {
            resp.sendRedirect("login.jsp?mid=" + rows);
        } else {
            req.setAttribute("msg", "注册失败");
            req.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(req, resp);
        }
    }
}
